/*
 *   This file is part of <open-parametrics>
 *   Copyright (c) 2006-2008 Miguel-Angel Sicilia
 *
 *   open-parametrics is free software: you can redistribute it and/or modify
 *   it under the terms of the Lesser GNU General Public License as
 *   published by the Free Software Foundation, either version 3 of
 *   the License, or (at your option) any later version.
 *
 *   open-parametrics is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with open-parametrics.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.uah.cc.ie.parametrics.softeng.examples;

import es.uah.cc.ie.parametrics.ArffDataset;
import es.uah.cc.ie.parametrics.CostDriver;
import es.uah.cc.ie.parametrics.CostDriverValue;
import es.uah.cc.ie.parametrics.CostEstimatingRelationship;
import es.uah.cc.ie.parametrics.Variable;
import es.uah.cc.ie.parametrics.regress.SimpleLinearRegressionCERGenerator;
import es.uah.cc.ie.parametrics.regress.SimpleNonLinearRegressionCERGenerator;
import es.uah.cc.ie.parametrics.regress.SimpleSingleLinearRegressionCER;
import es.uah.cc.ie.parametrics.regress.SimpleSingleNonLinearRegressionCER;
import es.uah.cc.ie.parametrics.softeng.SimpleExponentialSoftwareCER;
import java.util.LinkedList;
import java.util.List;


/**
 * A basic console program showing the general usage of the parametric
 * libraries.
 *
 * @author Miguel-Angel Sicilia
 */
public class SimpleCERExamples {

    public static void main(String args[]){

        // Define the cost drivers:
        CostDriver size = new CostDriver("size");

        // Define the CER and construct with the GRC model:
        CostEstimatingRelationship cer =
                new SimpleExponentialSoftwareCER("cer", 0.232, 1.43, size);

        // Create some data:
        CostDriverValue cdv = new CostDriverValue(size, 100);
        List<CostDriverValue> ins = new LinkedList<CostDriverValue>();
        ins.add(cdv);

        // Get the estimation:
        System.out.println("Estimated effort: " + cer.estimate(ins) );

        // Now get from Weka instances:
       cer =  new SimpleExponentialSoftwareCER("cer", 0.232, 1.43, size);

       ArffDataset ds = new ArffDataset("somedata", "datasets/abran.arff");
       ds.mapCostDriver(size, "size");
        // Get the estimation for all the instances:
       for (int i =0; i < ds.getSize();i++){
          ins =ds.getInstance(i);
          System.out.println("Estimated effort for instance "+
                  i+": " + cer.estimate(ins) );
       }

       // Now use a simple linear regression model:
       SimpleLinearRegressionCERGenerator g =
               new SimpleLinearRegressionCERGenerator("generator");
       Variable v = new Variable("cost-variable");
       ds.mapCostVariable(v, "effort");
       CostDriver[] x = new CostDriver[1];
       x[0]=size;
       SimpleSingleLinearRegressionCER gcer = 
               (SimpleSingleLinearRegressionCER)g.generate(x, v, ds);
       ins =ds.getInstance(0);
       System.out.println("Quality of linear adjustment:"+gcer.getR());
       System.out.println("Estimated effort for instance "+
                  0 +": " + gcer.estimate(ins) );

       // Now use an exponential function:
        SimpleNonLinearRegressionCERGenerator nlg =
               new SimpleNonLinearRegressionCERGenerator("generator");
       SimpleSingleNonLinearRegressionCER nlcer =
               (SimpleSingleNonLinearRegressionCER)nlg.generate(x, v, ds);
       System.out.println("Estimated effort for instance "+
                  0 +": " + nlcer.estimate(ins) );


    }
}
